package com.bbx.taxi.client.Adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.bbx.taxi.client.Bean.Value;
import com.bbx.taxi.client.DB.ContactsDB;
import com.bbx.taxi.client.DB.Util.DBcolumns;
import com.bbx.taxi.official.R;

/**
 * 常用联系人
 */
public class CommonContactAdapter extends BaseAdapter {
	private List<Map<String, String>> list;
	private Activity main;
	/** 常用联系人数据库 */
	private ContactsDB db_contacts;

	public CommonContactAdapter(Activity context,
			List<Map<String, String>> list) {
		this.list = list;
		main = context;

		db_contacts = new ContactsDB(main);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list != null ? list.size() : 0;
	}

	@Override
	public Map<String, String> getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView,
			ViewGroup parent) {
		ViewHolder viewholder;
		if (convertView == null) {
			LayoutInflater inflater = LayoutInflater.from(main);
			convertView = (View) inflater.inflate(
					R.layout.listitem_commoncontact, null);
			viewholder = new ViewHolder();
			viewholder.name = (TextView) convertView
					.findViewById(R.id.name);
			viewholder.telephone = (TextView) convertView
					.findViewById(R.id.telephone);
			viewholder.cb_collection = (CheckBox) convertView
					.findViewById(R.id.cb_collection);
			convertView.setTag(viewholder);
		} else
			viewholder = (ViewHolder) convertView.getTag();
		viewholder.name.setText(list.get(position).get(
				DBcolumns.CONTACTS_NAME));
		viewholder.telephone.setText(list.get(position).get(
				DBcolumns.CONTACTS_TEL));
		viewholder.cb_collection.setChecked(list.get(position)
				.get(DBcolumns.CONTACTS_COLLECTION).equals("1") ? true
				: false);
		viewholder.cb_collection
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton v,
							boolean isCheck) {
						if (!v.isPressed())
							return;
						if (isCheck) {
							onUpdate(
									list.get(position).get(
											DBcolumns.CONTACTS_ID),
									list.get(position).get(
											DBcolumns.CONTACTS_NAME),
									list.get(position).get(
											DBcolumns.CONTACTS_TEL), "1");
						} else {
							onUpdate(
									list.get(position).get(
											DBcolumns.CONTACTS_ID),
									list.get(position).get(
											DBcolumns.CONTACTS_NAME),
									list.get(position).get(
											DBcolumns.CONTACTS_TEL), "0");
						}
					}
				});
		return convertView;
	}

	public class ViewHolder {
		TextView name, telephone;
		CheckBox cb_collection;
	}
	/**
	 * 更新数据
	 */
	public void onUpdate(String id, String name, String tel, String collection) {
		db_contacts.onUpdate(id, name, tel, collection);
	}

}
